package io.renren.modules.roadLive.controller;

import com.baomidou.kisso.annotation.Login;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.roadLive.entity.RoadLiveEntity;
import io.renren.modules.roadLive.service.RoadLiveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author rdawnx
 * @package_name io.renren.modules.roadLive.controller
 * @class_name RoadLiveController.java
 * @author_email rdawnxhi@163.com
 * @JDK_version 1.8
 * @create_on 2019/07/24 21:00
 */
@RestController
@RequestMapping("roadlive/roadlive")
public class RoadLiveController {

    @Autowired
    private RoadLiveService roadLiveService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("roadlive:roadlive:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<RoadLiveEntity> roadLiveList = roadLiveService.queryList(query);
        int total = roadLiveService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(roadLiveList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("roadlive:roadlive:info")
    public R info(@PathVariable("id") Integer id){
        RoadLiveEntity roadLive = roadLiveService.queryObject(id);

        return R.ok().put("roadLive", roadLive);
    }

    /**
     * 保存
     */
    @Login
    @RequestMapping("/save")
    @RequiresPermissions("roadlive:roadlive:save")
    public R save(@RequestBody RoadLiveEntity roadLive, HttpServletRequest request){
//        String username = (String) request.getAttribute("username");
//        roadLive.setCreateUser(username);
        roadLive.setCreateTime(new Date());
        roadLiveService.save(roadLive);
        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    @RequiresPermissions("roadlive:roadlive:update")
    public R update(@RequestBody RoadLiveEntity roadLive){
        roadLive.setUpdateTime(new Date());
        roadLiveService.update(roadLive);
        return R.ok();
    }

    /**
     * 删除
     */
    @Login
    @RequestMapping("/delete")
    @RequiresPermissions("roadlive:roadlive:delete")
    public R delete(@RequestBody Integer[] ids){
        roadLiveService.deleteBatch(ids);

        return R.ok();
    }
}
